package com.erp.dao.impl;

import com.erp.bean.Role;
import com.erp.dao.RoleDao;
import com.erp.util.db.DataBaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ld
 * 角色数据访问实现类
 */
public class RoleDaoImpl extends DataBaseDao implements RoleDao {
    @Override
    public List<Role> findAll() {
        List<Role> list = new ArrayList<>();
        //1.创建SQL语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql = "select * from role";
            //2.创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //3.执行预编译指令得到结果集
            rs = pstmt.executeQuery();
            while(rs.next()){
                Role role=new Role(rs.getInt(1),rs.getString(2));
                list.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }

        return list;
    }

    @Override
    public int update(Role role) {
        int i=0;
        //1.创建SQL命令语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql =" update role set `rname`=? where rid=?";
            //2.创建预编译指令
            pstmt=conn.prepareStatement(sql);
            //3.执行预编译指令设置参数
            pstmt.setString(1, role.getRname());
            pstmt.setInt(2,role.getRid());
            //4.得到结果集
            i= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }

        return i ;
    }

    @Override
    public int deldte(int rid) {
        int i=0;
        //1.创建SQL命令语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql= "delete from role where rid=?";
            //2.创建预编译指令
            pstmt=conn.prepareStatement(sql);
            //3.执行预编译指令设置参数
            pstmt.setInt(1,rid);
            //4.得到结果集
            i= pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return i;
    }

    @Override
    public int add(Role role) {
        int i=0;

        try {
            //获取数据库连接对象
            buildConnection();
            //1.创建SQL命令语句
            String sql = "insert into role (rname) values (?)";
            //2.创建预编译指令
            pstmt=conn.prepareStatement(sql);
            //3.执行预编译指令得到结果集
            pstmt.setString(1, role.getRname());
            //4.得到结果集
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return i;
    }

    @Override
    public Role findByRid(int rid) {
        Role role=null;
        try {
            //获取数据库连接对象
            buildConnection();
            //1.创建SQL命令语句
            String sql = "select * from role where rid=?";
            //2.创建预编译指令
            pstmt=conn.prepareStatement(sql);
            //设置参数
            pstmt.setInt(1,rid);
            //3.执行预编译指令得到结果集
            rs=pstmt.executeQuery();
            //4.得到结果集
            while(rs.next()){
               role=new Role(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return role;
    }

}
